package cn.org.book.shop.app.controller;

import cn.org.book.shop.article.domain.ApArticle;
import cn.org.book.shop.article.domain.ApArticleDataConfiguration;
import cn.org.book.shop.article.service.IApArticleDataConfigurationService;
import cn.org.book.shop.article.service.IApArticleService;
import cn.org.book.shop.article.service.IApUserChannelService;
import cn.org.book.shop.common.core.domain.AjaxResult;
import cn.org.book.shop.common.core.page.TableDataInfo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 小程序分类管理
 * Created with IntelliJ IDEA.
 * User:  wanghongjie
 * Date:  2020/12/9 - 22:33
 * <p>
 * Description:
 */
@RequestMapping("/v1/app/channel/")
@RestController
public class ApChannelController extends AppBaseController {

    @Resource
    private IApUserChannelService channelService;

    @Resource
    private IApArticleService apArticleService;

    @Resource
    private IApArticleDataConfigurationService apArticleDataConfigurationService;

    @RequestMapping("getList")
    public AjaxResult getChannelList() {
        return AjaxResult.success(channelService.selectApUserChannelList(null));
    }

    @GetMapping("/channelList")
    public TableDataInfo channelList(ApArticle apArticle) {
        startPage();
        List<ApArticleDataConfiguration> apArticleDataConfigurations = new ArrayList<>();
        List<ApArticle> apArticles = apArticleService.selectApArticleList(apArticle);
        apArticles.forEach(item -> {
            apArticleDataConfigurations.addAll(test(item.getId()));
        });
        return getDataTable(changeApArticleDataConfiguration(apArticleDataConfigurations), apArticles);
    }

    private List<ApArticleDataConfiguration> test(Long aId) {
        ApArticleDataConfiguration apArticleDataConfiguration = new ApArticleDataConfiguration();
        apArticleDataConfiguration.setArticleId(aId);
        return apArticleDataConfigurationService.selectApArticleDataConfigurationList(apArticleDataConfiguration);
    }
}